﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.users
{
    public partial class order_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int payment_status;
        protected string keywords = string.Empty;
        protected string start_time = string.Empty;
        protected string end_time = string.Empty;
        public bool showdownload = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取管理员信息 20200116
            Model.manager admin = GetAdminInfo();

            if (admin != null)
            {
                if (admin.role_id == 1) { showdownload = true; }
            }

            this.payment_status = DTRequest.GetQueryInt("payment_status");
            this.keywords = DTRequest.GetQueryString("keywords");
            this.start_time = DTRequest.GetQueryString("start_time");
            this.end_time = DTRequest.GetQueryString("end_time");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                //RptBind("id>0" + CombSqlTxt(this.payment_status, this.start_time, this.end_time, this.keywords), "payment_status desc, id desc");
                RptBind("id>0" + CombSqlTxt(this.payment_status, this.start_time, this.end_time, this.keywords), " id desc");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (this.payment_status > 0)
            {
                this.ddlPaymentStatus.SelectedValue = this.payment_status.ToString();
            }
            this.txtStartTime.Text = this.start_time;
            this.txtEndTime.Text = this.end_time;
            txtKeywords.Text = this.keywords;
            BLL.member_order bll = new BLL.member_order();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("order_list.aspx", "payment_status={0}&start_time={1}&end_time={2}&keywords={3}&page={4}",
                this.payment_status.ToString(), this.start_time, this.end_time, this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(int _payment_status, string _start_time, string _end_time, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_payment_status > 0)
            {
                strTemp.Append(" and payment_status=" + (_payment_status==1?1:0));
            }
            _start_time = _start_time.Replace("'", "");
            if (!string.IsNullOrEmpty(_start_time))
            {
                strTemp.Append(" and datediff(d,add_time,'" + _start_time + "')<=0");
            }
            _end_time = _end_time.Replace("'", "");
            if (!string.IsNullOrEmpty(_end_time))
            {
                strTemp.Append(" and datediff(d,add_time,'" + _end_time + "')>=0");
            }

            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (order_no like '%" + _keywords + "%' or parent_mobile like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("order_list_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

       

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "payment_status={0}&start_time={1}&end_time={2}&keywords={3}",
                this.payment_status.ToString(), txtStartTime.Text, txtEndTime.Text, txtKeywords.Text));
        }

        //支付状态
        protected void ddlPaymentStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "payment_status={0}&start_time={1}&end_time={2}&keywords={3}",
                ddlPaymentStatus.SelectedValue, this.start_time.ToString(), this.end_time.ToString(), this.keywords));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("order_list_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "payment_status={0}&start_time={1}&end_time={2}&keywords={3}",
                this.payment_status.ToString(), this.start_time.ToString(), this.end_time.ToString(), this.keywords));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int sucCount = 0;
            int errorCount = 0;
            BLL.member_order bll = new BLL.member_order();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("order_list.aspx",
                "payment_status={0}&start_time={1}&end_time={2}&keywords={3}",
                this.payment_status.ToString(), this.start_time.ToString(), this.end_time.ToString(), this.keywords));
        }
    }
}